﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WF_QLThuVien.Entities.QuyDinh;
using System.Data.OleDb;
using WF_QLThuVien.Utilities;
using WF_QLThuVien.DataLayer;

namespace WF_QLThuVien.DataLayer.QuyDinh
{
    class CDAL_QuyDinh
    {
        DataAccessHelper db = new DataAccessHelper();
        
        public CQuyDinh GetQuyDinh()
        {
            try
            {
                CQuyDinh qd = db.GetQuyDinh("select * from QuyDinh");
                return qd;
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi CDAL_QuyDinh.GetQuyDinh \n" + ex.ToString());
            }
        }

        public bool Update(CQuyDinh qd)
        {
            try
            {
                int count = db.ExecuteNonQuery("UPDATE QuyDinh SET "
                                 + "SoSachMuonToiDa='" + qd.SoSachMuonToiDa
                                 + "', SoNgayMuonToiDa='" + qd.SoNgayMuonToiDa
                                 + "', SoLanGiaHan='" + qd.SoLanGiaHan
                                 + "', TienKyQuyToiThieu='" + qd.TienKyQuyToiThieu
                                 + "', PhiLamThe='" + qd.PhiLamThe
                                 + "', SoNgayMotLanGiaHan='" + qd.SoNgayMotLanGiaHan
                                 + "', TienPhatTreHan='" + qd.TienPhatTreHan
                                 + "', TienPhatMatSach='" + qd.TienPhatMatSach + "'");
                return count > 0;
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi CDAL_QuyDinh.Update \n" + ex.ToString());
            }
        }



























        
        /*
        #region: 1> Khai báo các thành viên dữ liệu.
        public OleDbConnection conn = null;
        public OleDbCommand cmd = null;
        #endregion

        public void Connet()
        {
            if (conn == null)
            {
                try
                {
                    conn = new OleDbConnection();
                    conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + CVariable.PATH;
                    conn.Open();
                    if (conn.State.ToString() != "Open")
                    {
                        conn.Open();
                    }
                }
                catch (Exception e)
                {
                    throw new Exception("loi ket noi");
                }
            }
        }
         public bool Update(CQuyDinh qd)
         {
             int count = dah.ExecuteNonQuery("update QuyDinh set "
                 +"SoSachMuonToiDa="+qd.SoSachMuonToiDa
                 +"SoNgayMuonToiDa="+qd.SoNgayMuonToiDa
                 +"SoLanGiaHan="+qd.SoLanGiaHan
                 +"TienKyQuyToiThieu="+qd.TienKyQuyToiThieu
                 +"PhiLamThe="+qd.PhiLamThe

         }

        public bool Update(CQuyDinh qd)
        {
            dah.Open();
            string strCom = "update QuyDinh set SoSachMuonToiDa =@soSachMuonToiDa,SoNgayMuonToiDa=@soNgayMuonToiDa,SoLanGiaHan=@soLanGiaHan,"
                + "TienKyQuyToiThieu=@tienKyQuyToiThieu,PhiLamThe=@phiLamThe,SoNgayMotLanGiaHan=@soNgayMotLanGiaHan,"
                + "TienPhatTreHan=@tienPhatTreHan,TienPhatMatSach=@tienPhatMatSach,TienPhatMotTrangSachMat=@tienPhatMotTrangSachMat";
            dah.cmd.Parameters.Add("@soSachMuonToiDa")

            Connet();
            try
            {

                string strCom = "update QuyDinh set "
                + "SoSachMuonToiDa=" + qd.SoSachMuonToiDa
                + "SoNgayMuonToiDa=" + qd.SoNgayMuonToiDa
                + "SoLanGiaHan=" + qd.SoLanGiaHan
                + "TienKyQuyToiThieu=" + qd.TienKyQuyToiThieu
                + "PhiLamThe=" + qd.PhiLamThe
                + "SoNgayMotLanGiaHan=" + qd.SoNgayMotLanGiaHan
                + "TienPhatTreHan=" + qd.TienPhatTreHan
                + "TienPhatMatSach=" + qd.TienPhatMatSach
                + "TienPhatMotTrangSachMat=" + qd.TienPhatMotTrangSachMat;
                cmd = new OleDbCommand(strCom, conn);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e)
            {
                throw new Exception("Lỗi Cập Nhật Quy Định");
            }

        }
        public CQuyDinh GetQuyDinh()
        {
            Connet();

            try
            {

                string strCom = "select * form QuyDinh";
                cmd = new OleDbCommand(strCom, conn);
                CQuyDinh qd = (CQuyDinh)cmd.ExecuteScalar();
                OleDbDataReader da = cmd.ExecuteReader();
                 while (da.Read())
                 {
                     CQuyDinh qd = new CQuyDinh();
                     qd.PhiLamThe = Double.Parse(da["PhiLamThe"].ToString());
                     qd.SoLanGiaHan = int.Parse(da["SoLanGiaHan"].ToString();
                     qd.SoNgayMotLanGiaHan = int.Parse(da["SoNgayMotLanGiaHan"].ToString();
                     qd.SoNgayMuonToiDa = int.Parse(da["SoNgayMuonToiDa"].ToString();
                     qd.SoSachMuonToiDa = int.Parse(da["SoSachMuonToiDa"].ToString();
                     qd.TienKyQuyToiThieu = Double.Parse(da["TienKyQuyToiThieu"].ToString();
                     qd.TienPhatMatSach = Double.Parse(da["TienPhatMatSach"].ToString();
                     qd.TienPhatMotTrangSachMat = Double.Parse(da["TienPhatMotTrangSachMat"].ToString();
                     qd.TienPhatTreHan = Double.Parse(da["TienPhatTreHan"].ToString();
                 }
                return qd;
            }
            catch (Exception e)
            {
                throw new Exception("Lỗi Cập Nhật Quy Định");
            }
        }
         */
    }
}
